import axios from 'axios'
import config from '@/config'

const baseUrl = process.env.NODE_ENV === 'development' ? config.baseUrl.dev : config.baseUrl.pro
//console.log(process.env.NODE_ENV)
export default function (option) {
    var instanceaxios = axios.create({
        baseURL: baseUrl,
        timeout: 5000
    });
    //配置请求和拦截
    instanceaxios.interceptors.request.use(config => {
        // Do something before request is sent
        // 1.当发送网络请求时, 在页面中添加一个loading组件, 作为动画

        // 2.某些请求要求用户必须登录, 判断用户是否有token, 如果没有token跳转到login页面
        //config.headers.Authorization = window.sessionStorage.getItem('token')
        // 3.对请求的参数进行序列化(看服务器是否需要序列化)
        // config.data = qs.stringify(config.data)
        return config;
    }, error => {
        // Do something with request error
        return Promise.reject(error);
    });
    instanceaxios.interceptors.response.use(response => {
        // Do something before response is sent
        return response.data;
    }, error => {
        // Do something with response error
        if(err && err.response){
            switch(err.response.status){
                case 400:
                    err.message='请求错误'
                    break;
                case 401:
                    err.message='未授权的访问'
                    break;
            }
        }
        return Promise.reject(error);
    });
    return instanceaxios(option)
}